<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="流程状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择流程状态" clearable>
                    <el-option v-for="dict in dict.type.oa_flow_billstatus" :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
<!--            <el-form-item label="申请标题" prop="applyTitle">-->
<!--                <el-input v-model="queryParams.applyTitle" placeholder="请输入申请标题" clearable @keyup.enter.native="handleQuery"/>-->
<!--            </el-form-item>-->
            <el-form-item label="单据编号" prop="billcode">
                <el-input v-model="queryParams.billcode" placeholder="请输入单据编号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="月度" prop="month">
                <el-date-picker clearable v-model="queryParams.month" type="month" value-format="yyyy-MM" placeholder="请选择月度"  @keyup.enter.native="handleQuery"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['oa:reimburseApply:add']">新增</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['oa:reimburseApply:remove']">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['oa:reimburseApply:export']">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table  :border="Global.tableShowBorder" v-loading="loading" ref="tables" :data="reimburseApplyList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"/>
                <el-table-column label="单据编号" align="center" prop="billcode" width="200"/>
                <el-table-column label="开始时间" align="center" prop="startTime" v-if="billType=='reimburse'" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="结束时间" align="center" prop="endTime" v-if="billType=='reimburse'" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="票据总数" align="center" prop="billCount" width="150"/>
                <el-table-column label="月度" align="center" prop="month" width="180"/>
    <!--            <el-table-column label="提交时间" align="center" prop="submitTime" width="180">-->
    <!--                <template slot-scope="scope">-->
    <!--                    <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>-->
    <!--                </template>-->
    <!--            </el-table-column>-->
                <el-table-column label="出差天数" align="center" prop="days" width="180" v-if="billType=='reimburse_bus'"/>
                <el-table-column label="出差地点" align="center" prop="city" width="180" v-if="billType=='reimburse_bus'"/>
                <el-table-column label="票据总金额" align="center" prop="billAmt">
                    <template slot-scope="scope">
                        <span style="font-size: 14px;font-weight: 900;color: red;">{{util.toEsStr(scope.row.billAmt)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="补贴总金额" align="center" prop="subsidyAmt" v-if="billType=='reimburse_bus'">
                    <template slot-scope="scope">
                        <span style="font-size: 14px;font-weight: 900;color: red;">{{util.toEsStr(scope.row.subsidyAmt)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="总金额" align="center" prop="totalAmt">
                    <template slot-scope="scope">
                        <span style="font-size: 14px;font-weight: 900;color: red;">{{util.toEsStr(scope.row.totalAmt)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="流程状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.oa_flow_billstatus" :value="scope.row.status"/>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-check" @click="doSubmitFlow(scope.row)" v-hasPermi="['oa:reimburseApply:edit']">提交审批</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa:reimburseApply:edit']">修改</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:reimburseApply:remove']">删除</el-button>

                        <el-button size="mini" type="text" v-show="scope.row.status=='1'"  icon="el-icon-refresh-right" @click="doCancleFlow(scope.row)" v-hasPermi="['oa:reimburseApply:edit']" style="color: #ff6666">撤销申请</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status!='0'"  icon="el-icon-rank"          @click="flowDetail(scope.row)" v-hasPermi="['oa:reimburseApply:edit']">流程信息</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status!='0'"  icon="el-icon-view"          @click="handleView(scope.row)" v-hasPermi="['oa:reimburseApply:edit']">查看</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
        </TableCard>

            <FlowInfo :businessId="businessId" :billType="billType" v-if="showFlowInfo" :showStatus="showFlowInfo" @closeFlowInfoDialog="closeFlowInfoDialog"></FlowInfo>

    </div>
</template>

<script>
import {delReimburseApply, listReimburseApply} from "@/api/oa/reimburseApply";
import FlowInfo from "@/views/workflow/flow/flowInfo";
import {submitFlow,cancleFlow} from "@/api/workflow/processInstance";

export default {
    name: "ReimburseApply",
    dicts: ['oa_flow_billstatus', 'reimburse_bill_type'],
    components:{FlowInfo},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            reimburseApplyList:[],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn:"createTime",
                isAsc:"descending",
                userId: null,
                deptId: null,
                status: null,
                applyTitle: null,
                billcode: null,
                billType: null,
                month: null,
            },
            showFlowInfo:false,
            //单据类型
            billType:"",
            businessId:""
        };
    },
    created() {
        const billType = this.$route.query && this.$route.query.billType;
        if(billType){
            this.queryParams.billType = billType;
            this.billType = billType;
        }
        this.getList();
    },
    methods: {
        /** 查询报销申请列表 */
        getList() {
            this.loading = true;
            listReimburseApply(this.queryParams).then(response => {
                this.reimburseApplyList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id
            this.$router.push({path: "/oa/reimburse/reimburseApply/form/" + row.id});
        },
        /** 查看单据详情 */
        handleView(row){
            this.$router.push({path: "/oa/reimburse/reimburseApply/form/" + row.id, query: {readonly:"1"}})
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$router.push({path: "/oa/reimburse/reimburseApply/form/add/"+this.billType})
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除报销申请编号为"' + ids + '"的数据项？').then(function () {
                return delReimburseApply(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },

        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/reimburseApply/export', {
                ...this.queryParams
            }, `reimburseApply_${new Date().getTime()}.xlsx`)
        },


        /** 提交审批 ***/
        doSubmitFlow(row){
            let data = {
                businessId:row.id,
                billType: row.billType
            }
            this.$modal.confirm('确认要提交该单据进行审批吗？').then( ()=> {
                return submitFlow(data);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("提交成功");
            }).catch(() => {
            });
        },
        /** 取回审批 ***/
        doCancleFlow(row){
            let data = {
                businessId:row.id,
                billType: row.billType
            }
            this.$modal.confirm('确认要取回该协议审批吗？').then( ()=> {
                cancleFlow(data).then(res=>{
                    if(200==res.code){
                        this.getList();
                        this.$modal.msgSuccess("取回成功");
                    }
                });
            })
        },
        /** 流程信息 ***/
        flowDetail(row){
            this.businessId = row.id+"";
            this.showFlowInfo = true;
        },
        //关闭流程信息窗口
        closeFlowInfoDialog(){
            this.showFlowInfo = false;
        },
    }
};
</script>
